package com.gofun.report.controller;


import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gofun.common.exception.BusinessException;
import com.gofun.common.exception.ExceptionCode;
import com.gofun.common.exception.TokenException;
import com.gofun.common.vo.ResponseEntity;

/**
 * 基础类
 * @author litianyi
 *
 * 2016年2月1日
 */
public class BaseController {
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	public static final int REDIS_KEY_TIME_OUT = 3 * 60 * 1000;
	
	public static final String REDIS_KEY_TRACK_UPDATE = "REDIS_KEY_TRACK_UPDATE";
	public static final String REDIS_KEY_STATUS_UPDATE = "REDIS_KEY_STATUS_UPDATE";
	public static final String REDIS_MOBILE_AREA = "REDIS_MOBILE_AREA";
	public static final String REDIS_KEY_ALERT_CAR_POSITON = "REDIS_KEY_ALERT_CAR_POSITON";
	
	/**
	 * 充电最小增长续航里程阀值（单位：米）
	 */
	public final static String STUB_MIN_ADD_MILEAGE = "STUB_MIN_ADD_MILEAGE";
	/**
	 * 充电检测时间间隔阀值（单位：分钟）
	 */
	public final static String STUB_INSPECT_TIME_INTERVAL = "STUB_INSPECT_TIME_INTERVAL";
	
	
	/**
	 * 参数丢失
	 * @param e
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(MissingServletRequestParameterException.class)
	public ResponseEntity handleMissingServletRequestParameterException(Exception e, HttpServletRequest request, HttpServletResponse response) {
//		logger.error(e.getMessage(), e);
		return buildResponseEntity(ExceptionCode.PARAM_LOST,"参数丢失"+e.getMessage(),null);
	}
	
	/**
	 * token 失败
	 * @param e
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(TokenException.class)
	public ResponseEntity tokenException(Exception e, HttpServletRequest request, HttpServletResponse response) {
//		logger.error(e.getMessage(), e);
//		token 失败
		return buildResponseEntity(ExceptionCode.TOKEN_FAILED,"token 验证失败",null);
	}
	
	/**
	 * 业务失败
	 * @param e
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@ResponseBody
	@ExceptionHandler(BusinessException.class)
	public ResponseEntity businessException(BusinessException e, HttpServletRequest request, HttpServletResponse response){
		logger.error(e.getMessage());
		return buildResponseEntity(ExceptionCode.BUSINESS_EXCEPTION);
	}
	/**
	 * 拦截器异常错误信息
	 * @param e
	 * @param request
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(Exception.class)
	public ResponseEntity handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
//		StringBuffer sb = new StringBuffer("信息:");
//		StackTraceElement[] error = e.getStackTrace();
//		if(error!=null){
//			for (StackTraceElement stackTraceElement : error) {
//				sb.append("["+stackTraceElement.getClassName()+"."+stackTraceElement.getMethodName()+":"+stackTraceElement.getLineNumber()+"]\r\n");
//			}
//		}
		logger.error(e.getMessage(),e);
		return buildResponseEntity(ExceptionCode.SYSTEM_EXCEPTION);
	}
	
	@ResponseBody
	@ExceptionHandler(IOException.class)
	public ResponseEntity handleIoException(Exception e, HttpServletRequest request, HttpServletResponse response) {
		logger.info(e.getMessage(),e);
		return buildResponseEntity(ExceptionCode.SYSTEM_EXCEPTION);
	}
	
	@ResponseBody
	@ExceptionHandler(Throwable.class)
	public ResponseEntity handleError(Exception e, HttpServletRequest request) {
		logger.error(e.getMessage(), e);
		return buildResponseEntity();
	}
	
	/**
	 * 生成ResponseEntity: 一般在系统异常的情况下使用
	 * @param description
	 * @param code
	 * @return
	 */
	protected ResponseEntity buildResponseEntity(){
		return buildResponseEntity(ExceptionCode.OK);
	}
	
	/**
	 * 生成ResponseEntity: 一般在系统异常的情况下使用
	 * @param description
	 * @param code
	 * @return
	 */
	protected ResponseEntity buildResponseEntity(ExceptionCode code){
		return buildResponseEntity(code, code.getDescription(),null);
	}
	
	protected ResponseEntity buildResponseEntity(ExceptionCode code,String description){
		return buildResponseEntity(code,description, null);
	}
	
	/**
	 * 生成ResponseEntity : 一般在业务出现异常的情况下使用
	 * @param businessCode
	 * @param description
	 * @param modelData
	 * @return
	 */
	protected ResponseEntity buildResponseEntity(ExceptionCode code,String description,Map<String, Object> modelData){
		return buildResponseEntity(code.getCode(),description, modelData);
	}
	
	/**
	 * 生成ResponseEntity : 一般在业务正常的情况下使用
	 * @param code
	 * @param modelData
	 * @return
	 */
	protected ResponseEntity buildResponseEntity(Map<String, Object> modelData){
		return buildResponseEntity(ExceptionCode.OK,ExceptionCode.OK.getDescription(), modelData);
	}
	
	/**
	 * 生成ResponseEntity
	 * @param code
	 * @param description
	 * @param modelData
	 * @return
	 * @throws Exception 
	 */
	protected ResponseEntity buildResponseEntity(Integer code,String description, Map<String, Object> modelData){
		ResponseEntity responseEntity = new ResponseEntity();
		responseEntity.setCode(code);
		responseEntity.setDesc(StringUtils.trimToEmpty(description));
		responseEntity.setModelData(modelData);
		try {
//			logger.info(JsonUtil.toJson(responseEntity));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
//			e.printStackTrace();
		}
		
		return responseEntity;
	}
	
}
